package org.picommo.common;

import java.io.PrintStream;

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

/**
 * Got fed up with configuring complex logging libraries when I only
 * needed some formatted printing with timestamps.
 *  
 * @author Magnus Wolffelt magnus.wolffelt@gmail.com
 *
 */
public final class Logger {
	private Logger() {}
	
	
	private static final DateTimeFormatter dtFormatter;
	private static final PrintStream output;
	
	static {
		dtFormatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm.ss");
		output = System.out;
	}
	
	
	public static void log(String message) {
		DateTime currentTime = new DateTime();
		output.println(currentTime.toString(dtFormatter) + ": " + message);
	}
	
	public static <E extends Throwable> void logError(E error) {
		log("Error: " + error.getMessage());
		error.printStackTrace(output);
	}
	
	public static <E> void log(E object, String objectName) {
		log(object.getClass().getSimpleName() + " " + objectName + ": " + object);
	}
	
	public static <E> void logChange(E before, E after) {
		if(!before.getClass().equals(after.getClass())) {
			throw new IllegalArgumentException("Not same type of objects when logging change");
		}
		
		log(before.getClass().getSimpleName() + " change: " + before + " -> " + after);
	}

}
